package com.fingard.dsp.bank.directbank.citicunion01;

import java.util.SortedMap;
import java.util.TreeMap;

import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.bankInterface.ICloseOrder;
import com.fingard.dsp.bank.reqrespobj.closeorder.CloseOrderState;
import com.fingard.dsp.bank.reqrespobj.closeorder.ReqCloseOrder;
import com.fingard.dsp.bank.reqrespobj.closeorder.RetAtsCloseOrder;
import com.fingard.net.ConnState;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;

public class CITICUnion01CloseOrder extends CITICUnion01Base implements ICloseOrder {
	public RetAtsCloseOrder closeOrder(ReqCloseOrder p_req) {
		RetAtsCloseOrder retAts = new RetAtsCloseOrder(p_req.reqHeader);
		try{
			retAts.rdSeq = p_req.rdSeq;
			ActSetItem actItem = this.getBankConfig().getActSetItemByAny(p_req.ownAct, p_req.merchantId, p_req.appId, "unifiedorder");
			if(actItem == null){
				retAts.respCode = "-1";
				retAts.respInfo = "未找到账户或商户或公众号对应的配置";
				return retAts;
			}
			SortedMap<String,String> returnMap = new TreeMap<String,String>();


			//接口类型：unified.trade.close
			returnMap.put("service", "unified.trade.close");
			//签名类型，取值RSA_1_256，默认RSA_1_256
			returnMap.put("sign_type", "RSA_1_256");
			//商户号
			String mch_id = p_req.merchantId;
			if (StringHelper.isNullOrEmpty(mch_id)) {
				mch_id = actItem.merchantId;
			}
			//接口类型：unified.trade.close
			returnMap.put("mch_id", mch_id);
			//订单流水号
			returnMap.put("out_trade_no", p_req.rdSeq);

			//32位随机码

			String nonce_str = get_nonce_str();
			returnMap.put("nonce_str", nonce_str);


			returnMap.put("sign", msgSign(returnMap, actItem.ownKeyStorePath));

			String[] tmpStrRet = sendToBank(parseXML(returnMap), "closeorder");//发送

			if(tmpStrRet[0].length() == 0){
				StringBuilder tmpSbMsg = new StringBuilder();
				XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);

				if(tmpBkReader != null){
					String return_code = tmpBkReader.getFirstText("/xml/status");
					String return_msg = tmpBkReader.getFirstText("/xml/message");
					if(return_code!= null && "0".equals(return_code)){
						boolean verifyResult = this.verifySign(tmpStrRet[1], actItem.oppCertFilePath, tmpSbMsg);
						if(verifyResult){
							String result_code = tmpBkReader.getFirstText("/xml/result_code");
							retAts.errorCode = tmpBkReader.getFirstText("/xml/err_code");
							retAts.errorMsg = tmpBkReader.getFirstText("/xml/err_msg");
							retAts.respInfo = StringHelper.joinNotEmpty("+", result_code, tmpBkReader.getFirstText("/xml/err_msg"));
									retAts.respCode = "0";
							if ("0".equals(result_code)) {
								retAts.closeState = CloseOrderState.Close_Success;
							} else {
								retAts.closeState = CloseOrderState.Close_Fail;
							}

							retAts.rdSeq = p_req.rdSeq;
						}
						else{
							retAts.respCode = "-1";
							retAts.respInfo = tmpSbMsg.toString();
						}
					}
					else{
						retAts.respCode = "-1";
						retAts.respInfo = return_msg;
					}
				}
				else{
					retAts.respCode = "-1";
					retAts.respInfo = tmpSbMsg.toString();
				}
			}
			else if(tmpStrRet[0].equals(ConnState.FAIL)){
				retAts.respCode = "0";
				retAts.closeState = CloseOrderState.Close_Fail;
				retAts.respInfo = tmpStrRet[1];
				retAts.errorCode = tmpStrRet[0];
				retAts.errorMsg = tmpStrRet[1];
			}
			else{
				retAts.respCode = "-1";
				retAts.respInfo = tmpStrRet[1];
			}
		}
		catch(Exception ex){
			ex.printStackTrace();
			retAts.respCode = "-1";
			retAts.respInfo = ex.getMessage();
			WriteBankLogLn(ex);
		}
		return retAts;
	}
}
